<template>
	<view class="content">
		<uni-segmented-control :current="tab" :values="items" @clickItem="onClickItem" style-type="text" active-color="#FF5E4D"></uni-segmented-control>
		<view class="">
			
		</view>
		<view class="order-list">
			<uni-load-more :status="loadStatus"></uni-load-more>
		</view>
		<view class="order-none" v-if ="false">
			<image class="order-none-img" src="/static/image/order.png" mode=""></image>
		</view>
		</view>
	</view>
</template>

<script>
import uniSegmentedControl from "@/components/uni-segmented-control/uni-segmented-control.vue"
// import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
import { orders, goods } from '@/config/mixins.js'
export default {
	mixins: [orders, goods],
	components: {
		uniSegmentedControl,
		// uniLoadMore
	},
	data() {
		return {
			items: [
				'全部',
				'待付款',
				'待收货',// '待发货',
				'已完成',//已发货',
				'已取消',//待评价',
				'待评价'
			],
			list: [],
			page: 1,
			limit: 5, // 每页订单显示数量
			loadStatus: 'more',
			status: [0, 1, 2, 3, 4]	,// 订单状态 0全部 1待付款 2，待收货 3。已完成 4已取消
			isReload: false, // 页面是否刷新?重载
		}
	},
	onLoad () {
		this.initData()
	},
	onShow () {
		// #ifdef MP-ALIPAY || MP-TOUTIAO
		let order_user_ship = this.$db.get('order_user_ship', true);
		if (order_user_ship) {
			this.isReload = order_user_ship;
			this.$db.del('order_user_ship', true);
		}
		let order_user_evaluate = this.$db.get('order_user_evaluate', true);
		if (order_user_evaluate) {
			this.isReload = order_user_evaluate;
			this.$db.del('order_user_evaluate', true);
		}
		// #endif
		if (this.isReload) {
			this.initData()
		}
	},
	computed: {
		// 获取订单列表tab
		tab () {
			return this.$store.state.orderTab
		}
	},
	methods: {
		// 初始化数据并获取订单列表
		initData (page = 1) {
			this.page = page
			this.list = []
			this.orderList()
		},
		// 订单状态切换
		onClickItem(index) {
			if (this.tab !== index) {
				this.$store.commit('orderTab', index)
				this.initData()
			}
		},
		// 获取订单列表
		orderList () {
			let data = {
				page: this.page,
				limit: this.limit,
				status: this.status[this.tab]
			}
			this.loadStatus = 'loading'
			this.$api.orderList(data, res => {
				if (res.status) {
					let _list = res.data.list
					if (res.data.status == this.status[this.tab]) {
						this.list = [...this.list, ...this.formatOrderStatus(_list)]
						// 判断所有数据是否请求完毕
						if (res.data.count > this.list.length) {
							this.page ++
							this.loadStatus = 'more'
						} else {
							this.loadStatus = 'noMore'
						}
					}
				} else {
					this.$common.errorToShow(res.msg)
				}
			})
			if (this.isReload) {
				this.isReload = false
			}
		},
		// 确认收货
		tackDelivery (index) {
			this.$common.modelShow('提示', '确认执行收货操作吗?', () => {
				let data = {
					order_id: this.list[index].order_id
				}
				this.$api.confirmOrder(data, res => {
					if (res.status) {
						this.$common.successToShow('确认收货成功', () => {
							if (this.tab !== 0) {
								this.list.splice(index, 1)
							} else {
								this.initData()
							}
						})
					} else {
						this.$common.errorToShow(res.msg)
					}
				})
			})
		},
		// 订单状态统一在这处理
		formatOrderStatus (orderList) {
			orderList.forEach (item => {
				switch (item.status) {
					case 1:
						if (item.pay_status === 1) {
							this.$set(item, 'order_status_name', '待付款')
						} else if (item.pay_status >= 2 && item.ship_status === 1){
							this.$set(item, 'order_status_name', '待发货')
						} else if (item.pay_status >= 2 && item.ship_status === 2){
							this.$set(item, 'order_status_name', '部分发货')
						} else if (item.pay_status >= 2 && item.ship_status >= 3 && item.confirm === 1) {
							this.$set(item, 'order_status_name', '已发货')
						} else if (item.pay_status >= 2 && item.ship_status >= 3 && item.confirm >= 2 && item.is_comment === 1) {
							this.$set(item, 'order_status_name', '待评价')
						} else if (item.pay_status >= 2 && item.ship_status >= 3 && item.confirm >= 2 && item.is_comment >= 2) {
							this.$set(item, 'order_status_name', '已评价')
						}
						break
					case 2:
						this.$set(item, 'order_status_name', '完成')
						break
					case 3:
						this.$set(item, 'order_status_name', '取消')
						break
				}
			});
			for(let i in orderList){
				for(let j in orderList[i].items){
					orderList[i].items[j].promotion_list = JSON.parse(orderList[i].items[j].promotion_list);
				}
			}
			return orderList
		}
	},
	// 页面下拉到底部触发
	onReachBottom () {
		if (this.loadStatus == 'more') {
			this.orderList()
		}
	}
}
</script>

<style>

</style>